import request from "@/utils/request";
import _ from "lodash";

const postTypeStatsDict = {
  1: "countLabour",
  2: "countOutsource",
  3: "countSocialSecurityPayment",
  4: "countPayrollAgency",
  5: "countOther"
};

const state = {
  postTypeLst: [
    { value: 1, label: "劳务派遣", title: "劳务派遣" },
    { value: 2, label: "外包", title: "外包" },
    { value: 3, label: "社保代缴", title: "社保代缴" },
    { value: 4, label: "薪资代发", title: "薪资代发" },
    { value: 5, label: "其他", title: "其他" }
  ],
  postTypeArr: ["劳务派遣", "外包", "社保代缴", "薪资代发", "其他"],
  postTypeStatsInitLst: [
    { value: "all", label: "在职员工", name: "countOnJob", count: 0 },
    { value: 1, name: postTypeStatsDict[1], count: 0, label: "劳务派遣" },
    { value: 2, name: postTypeStatsDict[2], count: 0, label: "外包" },
    { value: 3, name: postTypeStatsDict[3], count: 0, label: "社保代缴" },
    { value: 4, name: postTypeStatsDict[4], count: 0, label: "薪资代发" },
    { value: 5, name: postTypeStatsDict[5], count: 0, label: "其他" }
  ]
};

const mutations = {
  updatePostTypeLst(state, payload) {
    const lst = payload.map((item) => ({
      value: parseInt(item.dictItemValue),
      title: item.dictItemKey,
      label: item.dictItemKey
    }));
    const lst_sorted = _.sortBy(lst, ["value"]);

    state.postTypeLst = lst_sorted;
    state.postTypeArr = lst_sorted.map((item) => item.title);
    state.postTypeStatsInitLst = [
      state.postTypeStatsInitLst[0],
      ...lst_sorted.map((item) => ({
        ...item,
        count: 0,
        name: postTypeStatsDict[item.value]
      }))
    ];
  }
};

const actions = {
  async loadPostTypeLstAct({ commit }) {
    await request({
      url: `/admin/api/v1/dictItems/selectAllDictItem`,
      params: {
        dictCode: "POST_TYPE"
      }
    })
      .then((res) => {
        commit("updatePostTypeLst", res.data);
      })
      .catch((err) => {
        console.log("loadPostTypeLstAct", err);
      });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
